package com.cuiyun97.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuiyun97.annotation.AutoFill;
import com.cuiyun97.domain.dto.AdminDTO;
import com.cuiyun97.domain.dto.PageDTO;
import com.cuiyun97.domain.po.Admin;
import com.cuiyun97.domain.query.AdminQuery;
import com.cuiyun97.domain.vo.AdminVO;
import com.cuiyun97.enums.AutoFillType;
import com.cuiyun97.mapper.AdminMapper;
import com.cuiyun97.service.IAdminService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-12-06
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public PageDTO<AdminVO> queryPage(AdminQuery query) {

        Page<Admin> page = query.toMpPage();

        String username = query.getUsername();
        Page<Admin> p = lambdaQuery()
                .like(username != null, Admin::getUsername, username)
                .page(page);

        return PageDTO.of(p, admin -> {
            AdminVO vo = BeanUtil.copyProperties(admin, AdminVO.class);
            vo.setUsername(vo.getUsername().substring(0, vo.getUsername().length() - 2) + "**");
            return vo;
        });
    }

    @Override
    public AdminVO queryById(Integer id) {
        Admin admin = lambdaQuery()
                .eq(Admin::getId, id)
                .one();

        return BeanUtil.copyProperties(admin, AdminVO.class);
    }

    @AutoFill(value = AutoFillType.INSERT)
    @Override
    public void create(AdminDTO adminDTO) {
        // 时间已经自动处理, 这里不需要处理创建时间
        Admin admin = BeanUtil.copyProperties(adminDTO, Admin.class);
        save(admin);
    }

    @AutoFill(value = AutoFillType.UPDATE)
    @Override
    public void edit(AdminDTO adminDTO) {
        // 时间已经自动处理, 这里不需要处理更新时间
        // 配置了没有传值的, 不更新
        Admin admin = BeanUtil.copyProperties(adminDTO, Admin.class);
        updateById(admin);
    }

    @AutoFill(value = AutoFillType.DELETE)
    @Override
    public void deleteByIds(List<Integer> ids) {
        removeBatchByIds(ids);
    }

}
